'use strict';

const Controller = require('egg').Controller;

const jwt = require('jsonwebtoken')

class HomeController extends Controller {
  async index() {
    const { ctx } = this;
    ctx.body = 'hi, egg';
  }

  // 登陆
  async login() {
    const data = await this.service.home.login()
    if (data.length) {
      const { username, password, userid } = data[0];
      const token = jwt.sign({ username, password, userid }, this.app.config.keys, { expiresIn: '48h' })
      this.ctx.body = {
        token,
        code: 0,
        msg: '登陆成功'
      }
    } else {
      this.ctx.body = {
        code: -1,
        msg: 'fail'
      }
    }

  }

  // 注册
  async register() {
    this.ctx.body = 'register'
  }

  // 查询
  async findAll() {
    const data = await this.service.home.findAll();
    this.ctx.body = data;
  }

  // 查询url
  async findUrl() {
    const data = await this.service.home.findUrl();
    this.ctx.body = data;
  }
}

module.exports = HomeController;
